<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
class Index extends Controller
{
	public function user_management()
    {
        $view = $this->fetch("user_management");
        $ut = new Utiltool($view);
        return $ut->getpdata();
    }

	//查询
	public function getAllUser()
	{
        $result = Db::query("SELECT truckid,name,phone,identity,age,sex,lastdate
	            FROM all_user ORDER BY lastdate DESC");
        $ut = new Utiltool($result);
        return $ut->getpdata();
    }

	//增加
	public function addUser()
    {
        $user = Request::instance()->post();
        $user['password'] = md5($user['password']);
        $result = Db::table('all_user')->insert($user);
        $result_vehicle_load=['licensePlate'=>$user['truckid'],'volume'=>null,'owner'=>null,'phone'=>$user['phone']];
        $result_truck_week=['truckid'=>$user['truckid'],'Sunday'=>null,'Saturday'=>null,'frequency'=>null];
        $result_truck_position=['truckid'=>$user['truckid'],'longitude'=>null,'latitude'=>null,'positiontime'=>null];
        $result_vehicle_load1=Db::table('vehicle_load')->insert($result_vehicle_load);
        $result_truck_week1=Db::table('truck_week')->insert($result_truck_week);
        $result_truck_position1=Db::table('truck_position')->insert($result_truck_position);
        $ut = new Utiltool($result);
        return $ut->getpdata();
    }


	//删除
	public function deleteUser()
	{
	    $parameter = Request::instance()->post();
	    $result = Db::table("all_user")->where('truckid',$parameter['truckid'])->delete();
        $result2 = Db::table("vehicle_load")->where('licensePlate',$parameter['truckid'])->delete();
        $result3 = Db::table("truck_week")->where('truckid',$parameter['truckid'])->delete();
        $result1 = Db::table("truck_position")->where('truckid',$parameter['truckid'])->delete();
        $ut = new Utiltool($result);
        return $ut->getpdata();
	}


	//更改
	public function updateUser()
    {
        $data = Request::instance()->post();
        $result = Db::table("all_user")
                    ->where('phone', $data['phone'])
                    ->update([
                        'name' => $data['name'],
                        'username' => $data['username'],
                        'truckid' => $data['truckid'],
                        'identity' => $data['identity'],
                        'age' => $data['age'],
                        'sex' => $data['sex']
                    ]);
        $ut = new Utiltool($result);
        return $ut->getpdata();
    }
}